package com.wangshidai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangshidai.entity.SysDept;
import com.wangshidai.entity.SysUser;
import com.wangshidai.form.deptform.DeptTreeForm;
import com.wangshidai.mapper.SysUserMapper;
import com.wangshidai.service.SysDeptService;
import com.wangshidai.mapper.SysDeptMapper;
import com.wangshidai.util.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
    implements SysDeptService{
    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SysUserMapper userMapper;


    /**
     * 根据名称模糊查询的部门信息
     * @param searchName
     * @return
     */
    @Override
    public List<SysDept> listDept(String searchName) {
        // 如果没有输入，则直接返回所有
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        if (searchName != null){
            wrapper.like("name",searchName);
        }
        return deptMapper.selectList(wrapper);
    }

    /**
     * 根据id 查询部门信息
     * @param id
     * @return
     */
    @Override
    public SysDept selectById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public List<DeptTreeForm> queryAll() {
        return deptMapper.queryAll();
    }



    /**
     * 保存部门信息
     * @param dept
     */
    @Override
    public void saveDept(SysDept dept) throws BusinessException {
        log.info("{}",dept);
        //1.判断部门信息是否为空,为空则抛出异常信息
        if (dept == null){
            throw new  BusinessException("参数错误");
        }
        //2.同一上级下 部门名称不能重复
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("pid",dept.getPid());
        wrapper.eq("name",dept.getName());
        Long count = deptMapper.selectCount(wrapper);
        if (count>0){
            throw new BusinessException("部门名称重复");
        }
        deptMapper.insert(dept);
    }

    /**
     * 修改部门信息
     * @param dept
     */
    @Override
    public void updateDept(SysDept dept) throws BusinessException {
        log.info("{}",dept);
        //1.判断部门信息是否为空,为空则抛出异常信息
        if (dept == null){
            throw new  BusinessException("参数错误");
        }
        //2.同一上级下部门名称不能重复
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("pid",dept.getPid());
        wrapper.eq("name",dept.getName());
        wrapper.ne("id",dept.getId());
        Long count = deptMapper.selectCount(wrapper);
        if (count>0){
            throw new BusinessException("部门名称重复");
        }
        deptMapper.updateById(dept);
    }

    /**
     * 删除部门
     * 1.有子部门的部门不能删除
     * 2.有员工的部门不删除
     * @param id
     */
    @Override
    public void deleteDept(Integer id) throws BusinessException {
        //1.查询有子部门部门,如果有，则计数大于0
        QueryWrapper<SysDept> deptWrapper = new QueryWrapper<>();
        deptWrapper.eq("pid",id);
        Long deptCount = deptMapper.selectCount(deptWrapper);
        if(deptCount > 0){
            throw new BusinessException("该部门下有子部门，不能删除");
        }
        //查询该部门下是否有员工
        QueryWrapper<SysUser> userWrapper = new QueryWrapper<>();
        userWrapper.eq("dept_id",id);
        Long userCount = userMapper.selectCount(userWrapper);
        if(userCount > 0){
            throw new BusinessException("该部门下有员工，不能删除");
        }
        deptMapper.deleteById(id);
    }



}




